﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Spring.Rest.Client;
using System.Threading;
using Spring.Http.Converters;
using System.ComponentModel;

namespace ThoughtsFromThePot
{
    public partial class MainPage : PhoneApplicationPage
    {

        public ObservableCollection<Thought> thoughts = new ObservableCollection<Thought>();
        TFTPCLient client = new TFTPCLient();
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        public class Thought
        {

            public String Shitter { get; set; }
            public String ThoughtText { get; set; }
            public String TimeStamp { get; set; }

        }
        public class TFTPCLient
        {
            private RestTemplate restTemplate;
            private const string API_URL_BASE = "http://www.thoughtsfromthepot.com/";
            private const string GET_THOUGHTS = "GetThoughts.php";
            public TFTPCLient()
            {
                restTemplate = new RestTemplate(API_URL_BASE);
                restTemplate.MessageConverters.Add(new StringHttpMessageConverter());
            }
            public void GetThoughts(Action<IList<Thought>> operationCompleted)
            {
                JObject obj = new JObject();
                obj.Add("numberOfThoughts", "30");
                restTemplate.PostForObjectAsync<string>(GET_THOUGHTS, obj.ToString(), r =>
                {
                    IList<Thought> thoughts = ExtractThoughtsFromResponse(r.Response.ToString());
                    operationCompleted(thoughts);
                });

            }
            private IList<Thought> ExtractThoughtsFromResponse(String response)
            {
                IList<Thought> thoughts = new List<Thought>();

                JArray arr = new JArray();
                arr = JArray.Parse(response);
                IJEnumerable<JToken> query = arr.Children();
                String shitter;
                String thoughtText;
                String timeStamp;
                foreach (JObject item in query)
                {
                    shitter = item["shitter"].ToString();
                    thoughtText = item["thought"].ToString();
                    timeStamp = item["timeStamp"].ToString();
                    Thought thought = new Thought();
                    thought.Shitter = shitter.Substring(1, shitter.Length - 2);
                    thought.ThoughtText = thoughtText.Substring(1, thoughtText.Length - 2);
                    thought.TimeStamp = timeStamp.Substring(1, timeStamp.Length - 2);
                    thoughts.Add(thought);
                }
                return thoughts;
            }
        }
        private void listBox1_Loaded(object sender, RoutedEventArgs e)
        {

            

            client.GetThoughts(
                r =>
                {
                    this.ThoughtListBox.DataContext = r;
                });


        }

        private void ThoughtListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void postButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PostThought.xaml", UriKind.Relative));
        }





    }
}